package com.work.example.demo5.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.example.demo5.Mapper.AuthorMapper;
import com.work.example.demo5.Mapper.BookMapper;
import com.work.example.demo5.Mapper.CategoryMapper;
import com.work.example.demo5.Mapper.PressMapper;
import com.work.example.demo5.domain.BooksEntity;
import com.work.example.demo5.domain.formEntity;
import com.work.example.demo5.domain.vo.BookEntityVo;
import com.work.example.demo5.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, BooksEntity> implements BookService {
    @Autowired
    private AuthorMapper authorMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private PressMapper pressMapper;

    @Override
    public List<BookEntityVo> queryByForm(formEntity formEntity) {
        List<String> authorsList=new ArrayList<>();
        List<String> categoriesList=new ArrayList<>();
        List<String> pressesList=new ArrayList<>();
        List<String> returnedList=new ArrayList<>();
        for (String author:formEntity.getAuthors()) {
            int authorId=authorMapper.getauthorId(author);
            authorsList.add(String.valueOf(authorId));
        }
        for (String category:formEntity.getCategories()) {
            int categoryId=categoryMapper.getcategoryId(category);
            categoriesList.add(String.valueOf(categoryId));
        }
        for (String press:formEntity.getPresses()) {
            int pressId=pressMapper.getpressId(press);
            pressesList.add(String.valueOf(pressId));
        }
        for (String returned:formEntity.getReturneds()){
            if (returned.equals("在库"))
                returnedList.add("1");
            else
                returnedList.add("0");
        }
        List<BookEntityVo> bookEntityVos=baseMapper.queryByForm(formEntity.getName(),authorsList,pressesList,categoriesList,returnedList);
        System.out.println(bookEntityVos);
        return bookEntityVos;
    }

    @Override
    public List<BookEntityVo> getAllList() {
        List<BookEntityVo> bookEntityVos=baseMapper.getAllList();
        return bookEntityVos;
    }

    @Override
    public void removebook(Long bookId) {
        baseMapper.removebook(bookId);
    }

    @Override
    public void savebook(BooksEntity booksEntity) {
        baseMapper.savebook(booksEntity);
    }

    @Override
    public void updatebook(BookEntityVo bookEntityVo) {
        Long i = (long) authorMapper.getauthorId(bookEntityVo.getAuthorName());
        Long j = (long) pressMapper.getpressId(bookEntityVo.getPressName());
        Long k = (long) categoryMapper.getcategoryId(bookEntityVo.getCategoryName());
        String returned = bookEntityVo.getReturned();
        String returneds=new String("0");
        if (returned.equals("在库"))
            returneds=new String("1");
        BooksEntity booksEntity = new BooksEntity(bookEntityVo.getBookId(), bookEntityVo.getBookName(), i, j, k, returneds,null,null,null,null);
        baseMapper.updatebook(booksEntity);
    }


}
